First to do this we need to be able to get the poses of all the dots and the lamp.
In [7]:
    
%pylab inline
import rospy, tf
import vrep_common.srv
import geometry_msgs.msg, std_msgs.msg
import time
import matplotlib.pyplot as plt
import pylab
import numpy as np
    
    
    
In [2]:
    
rospy.init_node('point_illumination')
    
In [10]:
    
listener=tf.TransformListener()
    
In [13]:
    
trans, rot = listener.lookupTransform('/world', '/dummy0', rospy.Time(0))
trans
    
    Out[13]:
In [57]:
    
trans, rot = listener.lookupTransform('/world', '/Cuboid0', rospy.Time(0))
trans, rot
    
    Out[57]:
In [133]:
    
trans, rot = listener.lookupTransform('/world', '/head0', rospy.Time(0))
trans, rot
    
    Out[133]:
In [134]:
    
headtrans, headrot = trans, rot
    
simple light model: cone
In [135]:
    
headmat = listener.fromTranslationRotation(headtrans, headrot)
    
In [136]:
    
headmat * np.matrix([[0,
                    0,
                    0,
                    1]]).T
    
    Out[136]:
In [137]:
    
headmat * np.matrix([[1,
                    0,
                    0,
                    1]]).T
    
    Out[137]:
In [138]:
    
point0= headmat * np.matrix([[0,
                    0,
                    0,
                    1]]).T
    
In [139]:
    
direction_vector = headmat * np.matrix([[1, 0, 0, 1]]).T - point0
direction_vector
    
    Out[139]:
In [140]:
    
dummypos, dummyrot = listener.lookupTransform('/world', '/dummy0', rospy.Time(0))
dummypos
    
    Out[140]:
In [141]:
    
dummypos_vect = np.matrix([[dummypos[0], dummypos[1], dummypos[2], 1]]).T
dummypos_vect
    
    Out[141]:
In [142]:
    
direction_vector.A1
    
    Out[142]:
In [143]:
    
np.dot(direction_vector.A1, direction_vector.A1)
    
    Out[143]:
In [144]:
    
dist = np.dot(direction_vector.A1, dummypos_vect.A1)
dist
    
    Out[144]:
In [154]:
    
cone_len = 10.0
cone_r = 10.0
radius_at_point = (dist / cone_len) * cone_r
radius_at_point
    
    Out[154]:
In [154]:
    
    
In [146]:
    
dist_vect = dist * direction_vector
dist_vect
    
    Out[146]:
In [147]:
    
np.dot(dist_vect.A1, dist_vect.A1)
    
    Out[147]:
In [148]:
    
dist**2
    
    Out[148]:
In [149]:
    
rel_vect = dummypos_vect - point0
rel_vect
    
    Out[149]:
In [150]:
    
to_get_distance = (rel_vect - dist_vect).A1
    
In [151]:
    
ortho_dist = np.sqrt(np.dot(to_get_distance, to_get_distance))
ortho_dist
    
    Out[151]:
In [156]:
    
radius_at_point
    
    Out[156]:
In [152]:
    
tf.transformations.euler_from_matrix(headmat)
    
    Out[152]:
In [157]:
    
dummypos, dummyrot = listener.lookupTransform('/world', '/dummy0', rospy.Time(0))
dummypos_vect = np.matrix([[dummypos[0], dummypos[1], dummypos[2], 1]]).T
    
In [164]:
    
trans, rot = listener.lookupTransform('/world', '/head0', rospy.Time(0))
headtrans, headrot = trans, rot
headmat = listener.fromTranslationRotation(headtrans, headrot)
point0= headmat * np.matrix([[0, 0, 0, 1]]).T
direction_vector = headmat * np.matrix([[1, 0, 0, 1]]).T - point0
dummypos, dummyrot = listener.lookupTransform('/world', '/dummy0', rospy.Time(0))
dummypos_vect = np.matrix([[dummypos[0], dummypos[1], dummypos[2], 1]]).T
dist = np.dot(direction_vector.A1, dummypos_vect.A1)
cone_len = 10.0
cone_r = 10.0
radius_at_point = (dist / cone_len) * cone_r
rel_vect = dummypos_vect - point0
to_get_distance = (rel_vect - dist_vect).A1
ortho_dist = np.sqrt(np.dot(to_get_distance, to_get_distance))
print ortho_dist, radius_at_point
    
    
In [168]:
    
tf.transformations.quaternion_from_euler(0,0,np.pi)
    
    
In [169]:
    
tuple(tf.transformations.quaternion_from_euler(0,0,np.pi))
    
    Out[169]:
In [163]:
    
angle = np.pi/8
    
In [262]:
    
def orthodist_radatpoint(headpose, dummypose, cone_h=10.0, cone_r=10.0):
    headtrans, headrot = headpose
    dummytrans, dummyrot = dummypose
    headmat = listener.fromTranslationRotation(headtrans, headrot)
    point0= headmat * np.matrix([[0, 0, 0, 1]]).T
    direction_vector = headmat * np.matrix([[1, 0, 0, 1]]).T - point0
    dummypos_vect = np.matrix([[dummytrans[0], dummytrans[1], dummytrans[2], 1]]).T
    dist = np.dot(direction_vector.A1, dummypos_vect.A1)
    radius_at_point = (dist / cone_h) * cone_r
    rel_vect = dummypos_vect - point0
    to_get_distance = (rel_vect - dist_vect).A1
    ortho_dist = np.sqrt(np.dot(to_get_distance, to_get_distance))
    return ortho_dist, radius_at_point, dist
    
In [174]:
    
trans, rot = listener.lookupTransform('/world', '/head0', rospy.Time(0))
headtrans, headrot = trans, rot
dummypos, dummyrot = listener.lookupTransform('/world', '/dummy0', rospy.Time(0))
    
In [175]:
    
headtrans, rot
    
    Out[175]:
In [177]:
    
HEADTRANS
    
    Out[177]:
In [167]:
    
orthodist_radatpoint((headtrans, headrot), (dummypos, dummyrot))
    
    Out[167]:
In [170]:
    
def headpose_from_xyz_rot(x, y, z, rot):
    quat= tuple(tf.transformations.quaternion_from_euler(0,0,rot))
    return (x, y, z), quat
    
In [173]:
    
headpose_from_xyz_rot(headtrans[0], headtrans[1], headtrans[2], 0)
    
    Out[173]:
In [210]:
    
orthodist_radatpoint(
    headpose_from_xyz_rot(0, 0, 0, 0),
    headpose_from_xyz_rot(0, 0, 0, 0)
    )
    
    Out[210]:
In [183]:
    
np.linspace(-1.0, 1.0)
    
    Out[183]:
In [ ]:
    
headpose_from_xyz_rot(0, 0, 0, math.pi/2)
    
In [280]:
    
xs = []
ys = []
orthodists = []
radii = []
for x in np.linspace(-1.0, 1.0, 100):
    for y in np.linspace(-1.0, 1.0, 100):
        xs.append(x)
        ys.append(y)
        results  = orthodist_radatpoint(
            headpose_from_xyz_rot(0, 0, 0, math.pi/200),
            headpose_from_xyz_rot(x, y, 0, 0),
            10.,
            10.
            )
        orthodist = results[0]
        radius = results[1]
        dist = results[2]
        light = radius-orthodist
        orthodists.append(light)
        radii.append(radius)
    
In [283]:
    
plt.scatter(xs, ys, c=500.*np.array(orthodists), edgecolor='white', linewidths)
plt.colorbar()
    
    Out[283]:
    
In [287]:
    
img=np.ndarray(shape=(480, 640), dtype=float)
    
In [289]:
    
plt.imshow(img)
    
    Out[289]:
    
In [292]:
    
xs = np.linspace(-1.0, 1.0, 1000)
ys = np.linspace(-1.0, 1.0, 1000)
img=np.ndarray(shape=(len(ys), len(xs)), dtype=float)
orthodists = []
radii = []
for i in xrange(len(xs)):
    for j in xrange(len(ys)):
        x = xs[i]
        y = ys[j]
        
#         xs.append(x)
#         ys.append(y)
        results  = orthodist_radatpoint(
            headpose_from_xyz_rot(0, 0, 0, math.pi/200),
            headpose_from_xyz_rot(x, y, 0, 0),
            10.,
            10.
            )
        orthodist = results[0]
        radius = results[1]
        dist = results[2]
        light = radius-orthodist
        
        img[i][j] = light
#         orthodists.append(light)
#         radii.append(radius)
    
In [293]:
    
plt.imshow(img)
plt.colorbar()
    
    Out[293]:
    
In [248]:
    
plt.scatter(xs, ys, s=200.*np.array(orthodists))
    
    Out[248]:
    
In [243]:
    
plt.scatter(xs, ys, s=200.*np.array(radii))
    
    Out[243]:
    
    
In [180]:
    
# Generating a Gaussion dataset:
# creating random vectors from the multivariate normal distribution 
# given mean and covariance 
mu_vec1 = np.array([0,0])
cov_mat1 = np.array([[1,0],[0,1]])
X = np.random.multivariate_normal(mu_vec1, cov_mat1, 500)
R = X**2
R_sum = R.sum(axis=1)
plt.scatter(X[:, 0], X[:, 1], 
            color='gray', 
            marker='o', 
            s=32. * R_sum,
            edgecolor='black',
            alpha=0.5)
    
    Out[180]:
    
In [187]:
    
X.shape
    
    Out[187]:
In [220]:
    
headpose = headpose_from_xyz_rot(0, 0, 0, 0)
dummypose = headpose_from_xyz_rot(1, 1, 0, 0)
cone_h = 10.
cone_r = 10.
    
In [228]:
    
headtrans, headrot = headpose
dummytrans, dummyrot = dummypose
headmat = listener.fromTranslationRotation(headtrans, headrot)
point0= headmat * np.matrix([[0, 0, 0, 1]]).T
direction_vector = headmat * np.matrix([[1, 0, 0, 1]]).T - point0
dummypos_vect = np.matrix([[dummytrans[0], dummytrans[1], dummytrans[2], 1]]).T
dist = np.dot(direction_vector.A1, dummypos_vect.A1)
radius_at_point = (dist / cone_h) * cone_r
rel_vect = dummypos_vect - point0
to_get_distance = (rel_vect - dist_vect).A1
ortho_dist = np.sqrt(np.dot(to_get_distance, to_get_distance))
    
In [229]:
    
ortho_dist
    
    Out[229]:
In [230]:
    
headmat
    
    Out[230]:
In [226]:
    
direction_vector
    
    Out[226]:
In [ ]: